package com.jbm.core.iface;

import com.google.protobuf.ByteString;
import com.jbm.core.common.IoSessionShell;
import com.jbm.core.pb.PBIAcross;
import com.jbm.core.pb.PBIFileObject;
import com.jbm.core.pb.PBIP2P;

import java.math.BigDecimal;

/**
 * Created by root on 17-3-1.
 */
public class IP2P extends IAcross
{
	public IP2P()
	{
		super();
	}

	public IP2P(IAcross ia)
	{
		super(ia);
	}


	public IP2P(Integer fromer, Integer receiver, BigDecimal fee, String msg, String paytype,long timestamp )
	{
		this();

		m_receiver = receiver;
		m_fromer = fromer;
		m_msg = msg;
		m_fee=fee;
		m_paytype=paytype;
		m_timestamp=timestamp;

	}
	public IP2P(Integer fromer, Integer receiver, String msg, boolean initiate)
	{
		this();

		m_receiver = receiver;
		m_fromer = fromer;
		m_msg = msg;
		m_initiate = initiate;
	}

	public IP2P(Integer fromer, Integer receiver, String filepath, String filename, String filetype)
	{
		this();

		m_receiver = receiver;
		m_fromer = fromer;

		m_file = new IFileObject(filepath,filename);

		m_file.filetype = filetype;
	}

	public IP2P(Integer cg_id, Integer fromer, Integer receiver, String msg, boolean initiate)
	{
		this(fromer,receiver,msg,initiate);

		m_cg_id = cg_id;
	}
	public IP2P(int msg_id, int order_id,int task_Id, int fromer, int receiver,String type, String msg ,  long timestamp)
	{
       this.msg_id=msg_id;
		this.m_fromer=fromer;
		this.m_receiver=receiver;
		this.m_msg=msg;
		this.m_timestamp=timestamp;
		this.order_id = order_id;
		this.task_id=task_Id;
		this.type=type;


	}
	public IP2P(Integer cg_id, Integer fromer, Integer receiver, String filepath, String filename, String filetype)
	{
		this(fromer,receiver,filepath,filename,filetype);

		m_cg_id = cg_id;
	}

	public IP2P(Integer fromer, Integer receiver, String msg, String paytype,long timestamp)
	{
		this();

		m_receiver = receiver;
		m_fromer = fromer;

		m_msg = msg;

		m_paytype = paytype;
		m_timestamp=timestamp;
	}

	public IP2P(Integer fromer, Integer receiver, String filepath, String filename, String storename, String filetype)
	{
		this ( fromer,receiver,filepath,filename,filetype);

		m_file.storename = storename;
	}
	public int m_cg_id;
	public int m_fromer;
	public int m_receiver;
	public String m_msg;
	public BigDecimal m_fee;
	public String m_paytype;
	public boolean m_initiate;
    public int order_id;
	public int task_id;
	public String type;
   public int msg_id;

	public IFileObject m_file;

	public Object StartDecode(Object in, IoSessionShell ios) throws Exception
	{
		if ( m_file == null )
			return null;
		else
		{
			PBIP2P.IP2P.Builder fob = null;
			if ( in instanceof ByteString)
				fob = PBIP2P.IP2P.parseFrom((ByteString)in).toBuilder();
			else
				fob = ((PBIP2P.IP2P)in).toBuilder();

			PBIFileObject.IFileObject fo = (PBIFileObject.IFileObject)m_file.StartDecode(fob.getMFile(),ios);

			if ( fo != null )
			{
				fob.setMFile(fo);
				return fob.build();
			}

			return null;
		}
	}

	public Object PreEncode(Object obj) throws Exception
	{
		if ( m_file == null )
			return null;
		else
		{
			PBIP2P.IP2P p2p = null;
			if ( obj instanceof ByteString )
				p2p = PBIP2P.IP2P.parseFrom((ByteString)obj);
			else
				p2p = (PBIP2P.IP2P)obj;

			PBIFileObject.IFileObject fo = (PBIFileObject.IFileObject)m_file.PreEncode(p2p.getMFile());

			if ( fo == null )
				return null;
			else
				return p2p.toBuilder().setMFile(fo).build();
		}
	}
	public boolean Decode(Object in) throws Exception
	{
		if ( m_file == null )
			return false;
		else
			return m_file.Decode(in);
	}

	public boolean Encode(Object out, Object obj, PBIAcross.IAcross org) throws Exception
	{
		if ( m_file == null )
			return false;
		else
		{
			PBIP2P.IP2P p2p = null;
			if ( obj instanceof ByteString )
				p2p = PBIP2P.IP2P.parseFrom((ByteString)obj);
			else
				p2p = (PBIP2P.IP2P)obj;

			return m_file.Encode(out,p2p.getMFile(),org);
		}
	}
}
